var trNodeTag;
//delete Group
function deleteGroup(eventSrc){
	var trNode = $(getParentTR(eventSrc));
    var groupID = trNode.find("input[id='Id']").attr("value");
    
    $("#hiddenGroupID").attr("value",groupID);
    
	document.frmModifyGroup.action = "/EKMS-web/user/modifyGroup.do?action=delete";
	document.frmModifyGroup.submit();
}

//Pop up delete window
function whetherDelete(eventSrc,type){
	var whether = confirm("Do you want to delete this "+ type +"?");
	if (whether){
		if(type == "group"){
			deleteGroup(eventSrc);
		}
	}
}

//Input group information to the pop up window
function inputGroupInfo(trNode){
	trNodeTag = trNode;
	var groupId = trNode.find("input[id='Id']").attr("value");
	var groupName = trNode.find("input[id='GroupName']").attr("value");
	var groupStatus =  trNode.find("input[id='GroupStatus']").attr("value");
	var groupDesc =  trNode.find("input[id='GroupDesc']").attr("value");
	
	$("#ckbActiveUsers").attr("checked",false);
	$("input[id='hiddenGroupID']").attr("value",groupId);
	$("input[id='hiddenGroupName']").attr("value",groupName);
	$("select[id='hiddenGroupStatus']").attr("value",groupStatus);
	$("input[id='hiddenGroupStatus']").attr("value",groupStatus);
	$("textarea[id='hiddenGroupDesc']").attr("value",groupDesc);
	groupStatusChange();
}
//Pop up modify group window
function alertModifyGroup(eventSrc){	
	var trNode = $(getParentTR(eventSrc));
	inputGroupInfo(trNode);  
	showDiv();
	var groupData = {  
			"id":$("input[id='hiddenGroupID']").attr("value"),
			"groupName":$("input[id='hiddenGroupName']").attr("value")
	     };
	groupData = JSON2.stringify(groupData);
	var url ="../user/modifyGroup.do?action=initModifyGroup";
	
	 $.ajax({
        url: url,
        data: groupData,
        type: "POST",
        processData: true,
        contentType: "application/json",
        timeout: 20000,
        dataType: "json"});		
}



//Modify group. Send the request and data to action by AJAX
var message;
function modifyGroup(){
	$("#loadingPic").show();
	$("input").attr('disabled',' true');
	$("textarea").attr('disabled',' true');
	$("select").attr('disabled','true');
	$("#errors").empty();
	
	if($("#ckbActiveUsers").attr("checked")){
		isActiveOrInactiveUser = $("#ckbActiveUsers").val();
	}else{
		isActiveOrInactiveUser = "";
	}
	
	groupData = {  
			"id":$("input[id='hiddenGroupID']").attr("value"),
			"groupName":$("input[id='hiddenGroupName']").attr("value"),
			"status":$("select[id='hiddenGroupStatus']").attr("value"),
			"isActiveOrInactiveUser":isActiveOrInactiveUser,
			"description":$("textarea[id='hiddenGroupDesc']").attr("value")
	     };
		
	groupData = JSON2.stringify(groupData);
	var url ="../user/modifyGroup.do?action=modify";
	
	 $.ajax({
        url: url,
        data: groupData,
        type: "POST",
        processData: true,
        contentType: "application/json",
        timeout: 20000,
        dataType: "json",
        success: function(res){modifyGroupSucc(res);} ,
        error: function(message) { $("#loadmessage").append(message+"<br/>Error!Please refresh this page."); }
    });
}
//back the data to the initi time
function resetMethod(){
	var groupId = trNodeTag.find("input[id='Id']").attr("value");
	var groupName = trNodeTag.find("input[id='GroupName']").attr("value");
	var groupStatus =  trNodeTag.find("input[id='GroupStatus']").attr("value");
	var groupDesc =  trNodeTag.find("input[id='GroupDesc']").attr("value");
	
	$("#ckbActiveUsers").attr("value",status).attr("checked","");
	$("input[id='hiddenGroupID']").attr("value",groupId);
	$("input[id='hiddenGroupName']").attr("value",groupName);
	$("select[id='hiddenGroupStatus']").attr("value",groupStatus);
	$("input[id='hiddenGroupStatus']").attr("value",groupStatus);
	$("textarea[id='hiddenGroupDesc']").attr("value",groupDesc);
	$("#errors").hide();
	$("#noticeTxt").hide();
}
//Callback function when modify group successfully
function modifyGroupSucc(data){
	var res = data;
	message = data.notice;
	$("#noticeTxt").show();
	$("#noticeTxt").empty();
	$("#noticeTxt").append(data.notice);
	if(data.status == 0)
	{
		var groupId = data.group.groupId;
		//$("#autoCloseMsg").show();
		var ids = $("input[title='groupId']");
		$.each(ids,function(i){
			if(ids[i].value == groupId){
				var trNode = $(getParentTR(ids[i]));
				trNode.find("input[id='Id']").attr("value",data.group.groupId);
				trNode.find("input[id='GroupName']").attr("value",data.group.gruopName);
				trNode.find("input[id='GroupStatus']").attr("value",data.group.active);
				trNode.find("input[id='GroupDesc']").attr("value",data.group.description);
				trNode.find("span[id='GroupName']").html("<a onClick='alertModifyGroup(this)' class='handLink'>" + data.group.gruopName + "</a>");
				if(data.group.active == 0)
				{
					trNode.find("span[id='GroupStatus']").html("Yes");
				}else{
					trNode.find("span[id='GroupStatus']").html("No");
				}
				trNode.find("span[id='GroupDesc']").html(data.group.description);
			}
		});
		$("#errors").hide();
		resetGroupControls();
		//window.setTimeout(function(){resetGroupDialog();},5000);
	}else{
		$("#errors").show();
		$("#noticeTxt").hide();
		var errors =  data.errors;
		$.each(errors,function(i){
			$("#errors").append(errors[i].error + "<br/>");
		});
		resetGroupControls();
	}
}



//Query Group Request page by JS
function searchGroup(){
	document.frmModifyGroup.action = "/EKMS-web/user/modifyGroup.do?action=listGroup";
	document.frmModifyGroup.submit();
}

//Reset Group Controls
function resetGroupControls(){
	$("input").attr('disabled',false);
	$("textarea").attr('disabled',false);
	$("select").attr('disabled',false);
	$("#loadingPic").hide();
}

//Reset Group Dialog
function resetGroupDialog(){
	$("input").attr('disabled',false);
	$("textarea").attr('disabled',false);
	$("select").attr('disabled',false);
	$("#noticeTxt").empty();
	$(".succTxt").hide();
	$("#loadingPic").hide();
	$("#errors").hide();
	$("#noticeTxt").hide();
	closeDiv();
}


function groupDialogLoad(){
	
        if (typeof (window.dialogArguments) == undefined) {
            return;
        }
        $("input[id='GroupName']").attr("innerText",
            window.dialogArguments.groupName);   
}

//Change group active or inactive status on page
function groupStatusChange(){
	var statusId =  $("select[id='hiddenGroupStatus']").val();
	var status = $("select[id='hiddenGroupStatus'] option[value='" + statusId + "']").attr("title");

	$("#ckbActiveUsers").attr("value",status).attr("checked","");
	$("#activeNotice").html(" Do you want to "+"\'"+status+"\'"+" all the users under this group?");
}
function  check(field,limit)
{
  if (field.value.length >= limit)
    {
        field.value = field.value.substr(0,limit-1);
        field.focus();
     }
}

//Go to next page 
function goToNextPage(){
	var now = $("input[name='nowPage']").val();
	var total = $("input[name='totalPage']").val();
	if(parseInt(now) +1 <= total){
		document.frmModifyGroup.action = "/EKMS-web/user/modifyGroup.do?action=goToNextPage";
		document.frmModifyGroup.submit();
	}else{
		alert("This is the last page!");
	}
}

function goToPreviousPage(){
	var now = $("input[name='nowPage']").val();
	var total = $("input[name='totalPage']").val();
	if(parseInt(now) - 1 >= 1){
		document.frmModifyGroup.action = "/EKMS-web/user/modifyGroup.do?action=goToPreviousPage";
		document.frmModifyGroup.submit();
	}else{
		alert("This is the first page!");
	}
}

function goToPage(){
	var target = $("input[name='targetPage']").val();
	var total = $("input[name='totalPage']").val();
	if(typeof(target) != NaN && parseInt(target) > 0 && parseInt(target) <= parseInt(total) ){
		document.frmModifyGroup.action = "/EKMS-web/user/modifyGroup.do?action=goToTargetPage";
		document.frmModifyGroup.submit();
	}else{
		alert("The page number must be a number and between 0 and "+ total + "!");
	}
}

function goToFirstPage(){
	$("input[name='targetPage']").val(1);
	goToPage();
}

function goToLastPage(){
	var total = $("input[name='totalPage']").val();
	$("input[name='targetPage']").val(total);
	goToPage();
}

function bindGroupStatus(){
	var status = $("#hiddenGroupNameQuery").val();
	$("#groupActiveQuery").val(status);
}

function resetGroupSearch(){
	$("#groupNameQuery").attr("value","");
	$("#groupActiveQuery").val("");
}

function sortGroup(eventSrc){
	var type = eventSrc.title;
	$("#sortType").val(type);
	var order = $("#sortOrder").val();
	if(order == "asce"){
		sortOrder = "desc";
		$("#sortOrder").val("desc");
	}else{
		sortOrder = "asce";
		$("#sortOrder").val("asce");
	}
	document.frmModifyGroup.action = "/EKMS-web/user/modifyGroup.do?action=listGroup";
	document.frmModifyGroup.submit();
	
}